﻿//运行项目请前往：.\x64\Debug\Skyin Online_Judge BackServer.exe

/********************
  项目名称：Skyin Online_Judge BackServer
  项目描述：Skyin Online Judge BackServer是一个基于C++语言的后台服务器，用于处理用户提交的程序，并提供评测结果。
  项目作者：XTD Studio
  项目版本：Independent(IND)-Version 2.0-Student Version
  项目构建（更新）开始日期：2024-08-18
  项目发布日期：2024-09-03
  项目语言：C++
  项目开发环境：Windows 11 + Visual Studio 2022
  项目辅助开发：Fitten Code Editor
  项目文件：
  - x64
	- Debug
		- Skyin Online_Judge BackServer.exe
		- More...
  - Skyin Online_Judge BackServer.cpp
  - Skyin Online_Judge BackServer.sln
  - Skyin Online_Judge BackServer.vcxproj
  - Skyin Online_Judge BackServer.vcxproj.filters
  - Skyin Online_Judge BackServer.vcxproj.user
  - Skyin Online_Judge BackServer.rc
  - Skyin Online_Judge BackServer.aps
  - resource.h
  - update.cpp
  - update.h
  - new_page.cpp
  - password.cpp
  - password.h
  - main_page.cpp
  - main_page.h
  - Notepad.h
  - Notepad.cpp
 ********************/

#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <stack>
#include <fstream>
#include <ctime>
#include "resource.h" //资源文件
#include "update.cpp" //更新功能
#include "new_page.h" //新页面功能
#include "password.h" //密码保密功能
#include "main_page.h" //主页面UI
#include "Notepad.h" //记事本功能
using namespace std;

//用户基本信息
class user {
//private:
public:
	string name = "admin"; //用户名
	string password; //密码
	string email; //邮箱
	long long root_key = 0;
} admin;

string server_version = "HARMONY PC OS of SERVER v24.8"/*, check_key = "no_check_dev"*/, system_version = "IND-Version v2.0学生特别版";

bool check = false //是否激活
, init = false //是否开放服务器
, main_news = true; //主页新闻

string check_code = "20240829";

bool shutdown_q() { //注销服务器
	cout << "在注销服务器前，我们需要您完成一个注意事项的阅读，感谢您的配合！\n";
	Sleep(3000);
	system("cls");
	cout << "注销HARMONY PC OS of SERVER in <XTD_Skylight>服务器确认通知书\n=========================\n";
	cout << "尊敬的用户，您好！\n";
	cout << "	感谢您使用HARMONY PC OS of SERVER in <XTD_Skylight>服务器，您的使用对我们极为重要。为在后期对您产生不必要的麻烦，请仔细阅读本通知并确认！\n";
	Sleep(1000);
	cout << "1. 注销服务器后服务器不再可用，所有数据无法恢复！\n";
	cout << "2. 请确认您已经将服务器的公开情况转为 false。若没有，请在确认时输入 change -init 命令行完成情况转换。（使用此命令行默认确认注销）\n";
	cout << "3. 服务器注销后访问原域名将无法访问。若可以访问或一直加载，您的服务器公开状态还是处于 true。您必须想域名提供商申请注销域名。\n";
	cout << "4. 注销本服务器后所有造成的财产等损失本方均不负责。\n";
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1);
	cout << "5. 若您确认以上内容，请在下方命令行输入 shutdown -q 确认注销服务器。\n";
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	cout << "\n$HARMONY_SERVER/shutdown_q()_function/command> ";
	string temp_command;
	getline(cin, temp_command);
	if (temp_command == "shutdown -q") { //确认注销服务器
		return true;
	}
	else if (temp_command == "change -init") { //确认服务器公开
		init = false;
		return true;
	}
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	return false;
}

void run() {
	while (true) { //用户输入命令
		string command;
		cout << "\n$HARMONY_SERVER/" << admin.name << "/command> ";
		scanf_s("\n"); //假换行
		getline(cin, command);
		if (command == "exit") { //退出程序
			break;
		}
		else if (command == "help") { //帮助信息
			cout << "Tips: 若你输入了本页面命令，却无法使用，请确认系统激活状态！\n\n";
			cout << "# 帮助\n";
			cout << "  help: 显示帮助信息\n";

			cout << "# 系统操作\n";
			cout << "  load main_page: 加载主页面\n";
			cout << "  change news: 切换新闻显示状态\n";
			cout << "  exit: 退出程序\n";

			cout << "# 账户操作\n";
			cout << "  login: 登录系统\n";
			cout << "  register: 注册系统\n";
			cout << "  code: 激活码生成\n";
			//[隐藏]cout << "  SICH init: 生成SICH MT Key\n";

			cout << "# 信息\n";
			cout << "  user info: 查看用户信息\n";
			cout << "  system info: 查看系统信息\n";
			cout << "  follow new: 查看更新内容\n";
			if (check) cout << "  web net_list: 查看网络使用量\n";

			cout << "# 系统设置\n";
			cout << "  sudo root-main system[check]: 8位激活码: 激活系统\n";
			cout << "  server config: 配置服务器（开启公开访问无法使用）\n";
			cout << "  close: 关闭服务器访问\n";
			cout << "  start: 启动服务器访问\n";
			cout << "  shutdown -q: 注销服务器\n";

			cout << "# 页面\n";
			cout << "  new <languange>: 新页面\n";
			cout << "    <languange>列表: \n";
			cout << "      html: HTML\n      css: CSS\n      js: JavaSpirct\n      php: PHP\n      cpp: C++\n      py: Python\n      空串：记事本txt文件\n";
			cout << "  notepad: 记事本\n";
			if (check) cout << "  powershell: 带宏命令的命令控制台（无法使用）\n";

			cout << "# OJ操作信息\n";
			cout << "  dictionary: 序列字典（未开启公开访问无法使用）\n";
			cout << "  problem list: 题目列表（未开启公开访问无法使用）\n";
		}
		else if (command == "login") { //登录系统
			string temp_name, temp_password;
			cout << "用户名: ";
			getline(cin, temp_name);
			cout << "密码: ";
			temp_password = getPassword();
			if (temp_name == admin.name && temp_password == admin.password) { //登录成功
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
				cout << "\n登录成功！\n";
				if (admin.name == "root") //root用户
				{
					srand(time(0));
					admin.root_key = rand() % 255255255 + 100000000; 
					cout << "Root key已生成！\n";
				}
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
			}
			else { //登录失败
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
				cout << "\n用户名或密码错误！\n";
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
			}
		}
		else if (command == "register") { //注册系统
			string temp_name, temp_password, temp_email;
			int code;
			cout << "用户名: ";
			getline(cin, temp_name);
			cout << "密码（不支持空格）: ";
			temp_password = getPassword();
			//ofstream file("user.txt", ios::trunc); //验证码发送准备
			//file << temp_name << "\n" << temp_password << "\n";
			cout << "\n邮箱: ";
			getline(cin, temp_email);
			//file << temp_email << "\n";
			//file.close();
			//cout << "请在打开的Python文件中进行进一步操作。\n";
			//发送验证码，运行.py文件
			//system("send_code.py");
			//cout << "验证码已发送，请注意查收！\n";
			cout << "激活码（由8为数字组成，若暂不激活请输入0）: ";
			string temp_code;
			cin >> temp_code;
			if (temp_code == check_code) { //check用户
				check = true;
			}
			admin.name = temp_name, admin.password = temp_password, admin.email = temp_email;
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
			cout << "注册成功！\n";
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
		}
		else if (command.substr(0, 4) == "sudo" && !check && command.substr(30, command.length() - 30) == check_code) { //激活系统
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
			cout << "激活成功！" << admin.name << "祝您使用愉快！\n";
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
			check = true; //激活成功
		}
		else if (command == "change news") { //切换新闻显示状态
			if (main_news) main_news = false;
			else main_news = true;
		}
		else if (command == "user info") { //查看用户信息
			cout << "用户名: " << admin.name << "\n";
			cout << "邮箱: " << admin.email << "\n";
			cout << "激活状态: ";
			if (check) cout << "TRUE\n";
			else cout << "正式版本\n";
			if (admin.name == "root")
			{
				cout << "root key信息：\n";
				cout << admin.root_key << "\n";
			}
		}
		else if (command == "system info") { //查看系统信息
			cout << "服务器系统: " << server_version << "\n";
			cout << "操作系统：" << system_version << "\n\n";
			cout << "公网IP: 192.168.127.12\n";
			cout << "内网IP: 192.168.1.100\n";
			cout << "CPU: Intel i9-14710U\n";
			cout << "内存: 16G\n";
			cout << "硬盘: 512G SSD\n";
			cout << "激活状态：";
			if (check) cout << "已激活\n";
			else cout << "未激活\n";
			cout << "服务器状态：";
			if (init) cout << "开放\n";
			else cout << "关闭\n";
		}
		else if (check && admin.name == "root" && command == "SICH init") { //生成SICH MT Key
			string CLM;
			char key;
			cout << "Please input the CLM(Only numbers): ";
			cin >> CLM;
			cout << "Please input the CLM key(Only one character): ";
			cin >> key;
			//加密
			string output = CLM;
			for (size_t i = 0; i < output.size(); ++i) {
				output[i] ^= key;
			}
			cout << "Your SICH MT Key is: " << output << "\n";
		}
		else if (command == "code") { //激活码生成
			cout << "SICH MT Key（如何获取SICH MT Key？发送邮件至Chong_RailTransit获取）: ";
			string SICH;
			char key;
			getline(cin, SICH);
			cout << "请输入密钥（单个字符）: ";
			cin >> key;
			//解密
			string output = SICH;
			for (size_t i = 0; i < output.size(); ++i) {
				output[i] ^= key;
			}
			cout << "你的激活码为：" << output << "\n";
		}
		else if (check && command == "server config" && !init) { //配置服务器
			cout << "请输入root key：";
			long long temp_key;
			cin >> temp_key;
			if (temp_key == admin.root_key) { //验证成功
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
				cout << "\nroot key验证成功！\n";
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
				cout << "list\n-------------------\n";
				cout << "<A> 服务器域名配置（请确认已经购买此域名）\n";
				cout << "<B> 服务器系统升级\n";
				char choice;
				cout << "COMMAND> ";
				cin >> choice;
				if (choice == 'A') { //服务器域名配置
					string temp_domain;
					cout << "请输入服务器域名：";
					getline(cin, temp_domain); //假换行
					getline(cin, temp_domain);
					ofstream file("domain.txt", ios::trunc); //清空文件
					file << temp_domain; //写入文件
					file.close();
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
					cout << "服务器域名配置成功！\n";
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
				}
				else if (choice == 'B') { //服务器系统升级
					server_version = update(); //调用更新函数
				}
				else {
					cout << "暂无此功能！\n";
				}
			}
			else { //验证失败
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
				cout << "\nroot key验证失败！\n";
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
			}
		}
		else if (check && command.substr(0, 3) == "new") { //新页面
			if (command.size() == 3) { //无参数
				notepad(); //默认记事本
				continue;
			}
			string temp_cmd = command.substr(4, command.length() - 4);
			if (temp_cmd == "html") { //HTML页面
				new_html();
			}
			else if (temp_cmd == "css") { //CSS页面
				new_css();
			}
			else if (temp_cmd == "js") { //JavaSprict页面
				new_js();
			}
			else if (temp_cmd == "php") { //PHP页面
				new_php();
			}
			else if (temp_cmd == "cpp") { //C++文件
				cpp();
			}
			else if (temp_cmd == "py") { //Python文件
				python();
			}
			else { //记事本txt文件
				notepad();
			}
		}
		else if (check && command == "close") { //关闭服务器访问
			init = false;
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
			cout << "命令执行完成！\n";
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
		}
		else if (check && command == "start") { //启动服务器访问
			init = true;
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
			cout << "命令执行完成！\n";
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
		}
		else if (command == "shutdown -q") { //注销服务器
			cout << "请输入root key: ";
			long long temp_key;
			cin >> temp_key;
			if (temp_key == admin.root_key) { //验证成功
				system("cls");
				if (shutdown_q()) { //注销成功
					cout << "正在为你注销……\n";
					Sleep(3000);
					//清空文件
					ofstream file("domain.txt", ios::trunc);
					file.close();
					exit(0); //退出程序
				}
				else
				{
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
					cout << "你似乎没有确认注销服务器的权限。请重新使用本命令！\n";
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
				}
			}
			else { //验证失败
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
				cout << "\nroot key验证失败！\n";
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
			}
		}
		else if (command == "dictionary" && init) { //序列字典
			for (int i = 1; i <= 3; i++) //加载
			{
				cout << "拉取用户信息中，Waiting";
				Sleep(300);
				cout << '.';
				Sleep(300);
				cout << '.';
				Sleep(300);
				cout << '.';
				system("cls");
			}
			int st, nd, rd, th;
			bool light = false;
			srand(time(0));
			for (int i = 1; i <= rand() % 1500 + 300; i++) //输出用户序列
			{
				st = rand() % 255 + 1, nd = rand() % 255 + 1, rd = rand() % 255 + 1, th = rand() % 255 + 1, light = rand() % 2;
				if (light) printf("[Online]  IPconfig: -%d.%d.%d.%d-\n", st, nd, rd, th); //在线序列
				else printf("[Offline] IPconfig: -%d.%d.%d.%d-\n", st, nd, rd, th); //离线序列
				Sleep(rand() % 40 + 10);
			}
		}
		else if (command == "problem list" && init) { //题目列表
			for (int i = 1; i <= 3; i++) //加载
			{
				cout << "拉取题目信息中，Waiting";
				Sleep(300);
				cout << '.';
				Sleep(300);
				cout << '.';
				Sleep(300);
				cout << '.';
				system("cls");
			}
			srand(time(0));
			cout << "PROBLEM list: \n";
			for (int i = 1; i <= rand() % 800 + 600; i++) //输出题目
			{
				int AC = rand() % (800 - i), WA = rand() % (200 + i) + 100, other = rand() % 200 + 1;
				printf("[Problem %3d] AC: %3d   WA: %3d   other: %3d\n", i, AC, WA, other);
				Sleep(rand() % 30 + 5);
			}
		}
		else if (check && command == "web net_list") { //网络数据使用情况
			cout << "服务器网络数据使用情况查询系统\n========================================\n";
			string temp_ip;
			cout << "服务器公网IP地址：";
			cin >> temp_ip;
			if (temp_ip == "192.168.127.12") { //验证成功
				for (int i = 1; i <= 3; i++) //加载
				{
					cout << "正在查询信息，Waiting";
					Sleep(300);
					cout << '.';
					Sleep(300);
					cout << '.';
					Sleep(300);
					cout << '.';
					system("cls");
				}
				srand(time(0));
				time_t now = time(0); //获取当前时间
				char* dt = ctime(&now);
				cout << dt << "使用数据列表：\n-----------------------\n";
				cout << "使用总量：" << rand() % 2048 + 512 << "GB\n";
				cout << "最近24小时使用量：" << rand() % 64 + 4 << "GB\n";
				cout << "使用运营商：中国移动\n";
				cout << "运营商代码：10086\n";
				cout << "数据使用差量：" << rand() % 512 << "GB\n";
				cout << "剩余数据流量：" << rand() % 509601024 + 102400000 << "GB\n";
			} else {
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
				cout << "未查找到相关IP或您输入的IP地址不正确！\n";
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
			}
		}
		else if (command == "load main_page") system("cls"), main_page(main_news, system_version);
		else if (command == "follow new") { //更新内容
			cout << "2024-08-18\n\
				- 增加激活码生成\n\
				- 修改激活机制，并分离change功能\n";
		}
		else if (command == "notepad") { //记事本
			notepad();
		}
		else if (check && command == "powershell") { //命令控制台
			system("cls");
			cout << "$HARMONY_SERVER/" << admin.name << "/Powershell> ";
			string cmd;
			getline(cin, cmd);
			if (cmd == "/exit 0") main_page(main_news, system_version);
		}
		else if (command == "hyper xtd_skylight") { //隐藏彩蛋
			system("cls"); //清屏
			cout << admin.name << ": 咦？\n";
			Sleep(1000);
			cout << admin.name << ": 你在做什么？\n";
			Sleep(3000);
			cout << "<神秘...>: 你可以猜猜。\n";
			Sleep(2000);
			cout << admin.name << ": [观察中……]\n";
			Sleep(3000);
			cout << admin.name << ": 这好像是最新发布的XTD_Skylight，你不会是……XTD Studio吧？\n";
			Sleep(3000);
			cout << "<XTD Studio>: 你猜对了！\n";
			Sleep(3000);
			cout << admin.name << ": 听说这是XTD Studio的最新作品！你能给我介绍一下吗？\n";
			Sleep(3000);
			cout << "<XTD Studio>: 嗯，XTD_Skylight是XTD Studio的最新作品，它是一款基于XTD Studio的开源软件，你可以在Gitee上找到它。\n";
			Sleep(5000);
			cout << admin.name << ": 谢谢！然后呢？\n";
			Sleep(3000);
			cout << "<XTD Studio>: XTD_Skylight搭载了全新的引擎，你可以在你的PC上更流畅地使用Skyin OJ BackServer！\n";
			Sleep(5000);
			cout << admin.name << ": 太好了！我一定要试试！\n";
			Sleep(3000);
			cout << "<XTD Studio>: 祝你好运！\n";
			Sleep(3000);
			cout << admin.name << ": 谢谢！\n";
			Sleep(3000);
			cout << "<XTD Studio>: 再见！\n";
			Sleep(3000);
			cout << admin.name << ": 再见！\n";
			Sleep(3000);
			cout << "注：部分对话内容由Fitten Code(AI)生成。\n";
			Sleep(1000);
			system("cls");
			Sleep(1000);
			cout << "全新的大门即将开启……\n";
			Sleep(3000);
			system("cls"); //清屏
			main_page(main_news, system_version); //刷新页面
		}
		else { //未知命令
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
			cout << "暂无或你无权使用此命令或未开启某些功能，请检查后再重试！\n";
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
		}
	}
}

void start_page() { //启动页面
	system("cls"); //清屏
	while (true)
	{
		string command;
		cout << "$main|startroot/structure> ";
		cin >> command;
		if (command == "confirm") { //启动命令
			break;
		}
		else {
			cout << "\n";
		}
	}
}

int main() {
	while (true)
	{
		start_page();
		system("cls");
		Sleep(2000);
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1);
		cout << "  |  S";
		Sleep(200);
		cout << "k";
		Sleep(200);
		cout << "y";
		Sleep(200);
		cout << "i";
		Sleep(200);
		cout << "n";
		Sleep(200);
		cout << "O";
		Sleep(200);
		cout << "J ";
		Sleep(200);
		cout << "BackServer  |  \n  |                      |  \n  |                      |  \n";
		Sleep(1000);
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
		cout << "  |------";
		Sleep(400);
		cout << "Powered by";
		Sleep(400);
		cout << "------|  \n";
		Sleep(1000);
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
		cout << "  |   XTD_Skylight(OS)   |  \n";
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
		ifstream den("dependencies.DCI"); //依赖文件检测
		if (!den) { //依赖文件不存在
			ofstream file("dependencies.DCI", ios::trunc); //创建文件
		}
		Sleep(3000);
		system("cls");
		main_page(main_news, system_version);
		run(); //控制台程序入口
		cout << "正在注销，请稍等……\n";
		Sleep(3000);
	}
	return 0;
}